public with sharing class AccountServiceAreaZipcodetriggerHandler {


static Set<Id> accaddressupdateid=new Set<Id>();
static Set<Id> accaddressinsertid=new Set<Id>();

public void AccountServiceAreaZipcodeInsert(List<Service_Area_Zip_Codes__c> acczipList){
     
    Set<Id> accountId=new Set<Id>();
    Set<Id> zipId=new Set<Id>();
    Set<String> postalcode=new Set<String>();
    List<Service_Area_Zip_Codes__c> servicezipList=new List<Service_Area_Zip_Codes__c>();
    List<Service_Area_Zip_Codes__c> servicezipInsertList=new List<Service_Area_Zip_Codes__c>();
    if(acczipList != null && acczipList.size()>0){
        for(Service_Area_Zip_Codes__c zip: acczipList){
                accountId.add(zip.Affiliate_Account__c);
                postalcode.add(zip.Postal_Code__c);
                zipId.add(zip.Id);
                 }
        }  
      servicezipList=[select Id,Affiliate_Account__c ,Postal_Code__c from Service_Area_Zip_Codes__c where Affiliate_Account__c IN: accountId and Postal_Code__c IN: postalcode and Id not IN: zipId];
      
      if(servicezipList!=null && servicezipList.size()>0){

        	for(Service_Area_Zip_Codes__c zipold:servicezipList){
        	for(Service_Area_Zip_Codes__c zipnew:acczipList){
        		system.debug('@@zipold.Affiliate_Account__c'+(zipold.Affiliate_Account__c));
        		system.debug('@@zipnew.Affiliate_Account__c '+(zipnew.Affiliate_Account__c ));
        		system.debug('@@zipold.Postal_Code__c'+(zipold.Postal_Code__c));
        		system.debug('@@zipnew.Postal_Code__c  '+(zipnew.Postal_Code__c  ));
        		
        	 	if(zipnew.Affiliate_Account__c != zipold.Affiliate_Account__c && zipnew.Postal_Code__c != zipold.Postal_Code__c){
        	 	 servicezipInsertList.add(zipnew);
        	 	 system.debug('@@servicezipInsertList'+servicezipInsertList);
        	 	}
        	 	else{
        	 	  //system.debug('Service area zipcode is already exist');
        	 	 
        	 	  Trigger.new[0].addError('Service area zipcode already exists'); 
        	 }
        }
       }
              //Trigger.new[0].addError('Service area zipcode is already exist');  
              if(servicezipInsertList!=null && servicezipInsertList.size()>0){
              	insert servicezipInsertList;
              }
      }
    
    
}
}